import json
import os
import sys

BASE_DIR = os.path.dirname(os.path.abspath(__file__))  # 当前文件所在目录
CONFIG_PATH = os.path.join(BASE_DIR, '../config/config.json')  # 拼接成绝对路径

class ConfigReader:
    def __init__(self, config_path=CONFIG_PATH):
        self.config_path = config_path
        self.config_data = self._load_config()

    def _load_config(self):
        if not os.path.exists(self.config_path):
            raise FileNotFoundError(f"Config file not found at {self.config_path}")
        
        with open(self.config_path, 'r') as file:
            return json.load(file)

    def get(self, key, default=None):
        keys = key.split('.')
        value = self.config_data
        try:
            for k in keys:
                value = value[k]
            return value
        except KeyError:
            if default is not None:
                return default
            raise KeyError(f"Key '{key}' not found in configuration")


